#include <stdio.h>
#include <stdlib.h>
#include <csp_spu.h>
#include <csp_debug.h>
#include "mc_pi.h"
#include "config.h"

int global_pid; 

int Run(){
	int i;
	double sum=0;
	int p_iterations __attribute__ ((aligned(128)));
	double sum_part __attribute__ ((aligned(128)));

	csp_channel master2slaves, slaves2master;


	/* et lignende lipspe-program skal dele en kø med tasks */
	master2slaves = csp_create_channel(ONE2ANY_CHANNEL, MASTER2SLAVES, sizeof(int));
	slaves2master = csp_create_channel(ANY2ONE_CHANNEL, SLAVES2MASTER, sizeof(double));

	printf("N=%i\n", N);
	p_iterations = (ITERATIONS / N) + (ITERATIONS % N);
	for(i=0; i < N; i++){
		csp_channel_write(master2slaves, &p_iterations);
		p_iterations = (ITERATIONS / N);
	}

	for(i=0; i < N; i++){
		csp_channel_read(slaves2master, &sum_part);
		//printf("master recieved sum: %f\n", sum_part);
		sum += sum_part;
	}

	printf("MC-PI is %f, sum=%f, iterations=%i, N_WORKERS=%i\n", 4*sum/(double)ITERATIONS, sum, ITERATIONS, N); 

	return 0;
}
